package org.luxor.cloud.portal.config;

import org.luxor.commons.security.config.WebSecurityProperties;
import org.luxor.commons.security.constant.SecurityConstants;
import org.luxor.commons.security.utils.SecurityUtils;
import org.springframework.boot.autoconfigure.security.oauth2.client.EnableOAuth2Sso;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;

import javax.annotation.Resource;

/**
 * 身份认证规则配置
 *
 * @author Mr.Yan  @date: 2020/9/8
 */
@Configuration
@EnableOAuth2Sso
@EnableConfigurationProperties({WebSecurityProperties.class})
public class WebSsoSecurityConfigurer extends WebSecurityConfigurerAdapter {

    @Resource
    private WebSecurityProperties properties;

    public WebSsoSecurityConfigurer() {
    }

    /**
     * 配置鉴权机制
     *
     * @param http
     * @throws Exception
     */
    @Override
    public void configure(HttpSecurity http) throws Exception {

        // 允许使用iframe 嵌套，避免swagger-ui 不被加载的问题
        http.headers().frameOptions().disable()
                .and().anonymous().principal(SecurityUtils.ANONYMUS)
                .and().authorizeRequests()
                .antMatchers(SecurityConstants.DEFAULT_ANON_URIS).permitAll()
                .antMatchers(properties.getAnonUris()).permitAll()
                .anyRequest().authenticated()
                .and().csrf().disable();
    }

}
